﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LevelEditor.Objects;
using LevelEditor.Objects.Tiles;
using LevelEditor.Objects.Waypoints;
using LevelEditor.Dao;
using DialogsEditor.DAO;
using DialogsEditor;

namespace LevelEditor
{
    public partial class WaypointsConfiguration : Form
    {

        public static bool IsCreatingWaypointConfiguration;

        private Behavior selectedAction;
        public Behavior SelectedAction
        {
            get { return selectedAction; }
        }

        private WaypointsCollection waypoints;
        public WaypointsCollection Waypoints
        {
            get { return waypoints; }
        }

        private Behavior selectedFinalBehavior;

        private DialogEntity selectedDialog;

        private bool wantToClose;

        private DialogResult result;

        public WaypointsConfiguration(Character c) // send a character to fill the action selection
        {
            InitializeComponent();
            result = System.Windows.Forms.DialogResult.None;

            this.FormClosing += WaypointsConfiguration_FormClosing;

            LoadBehaviors();
            LoadEndWith();
            LoadDialogs();

            waypoints = new WaypointsCollection();
        }

        private void LoadBehaviors()
        {
            List<Behavior> behaviors = LevelEditorDAO.GetInstance().GetAllWalkBehavior();
            

            for (int k = 0; k < behaviors.Count; k++)
            {
                actionSelector.Items.Add(behaviors[k].Name);
            }
        }

        private void LoadEndWith()
        {
            List<Behavior> behaviors = LevelEditorDAO.GetInstance().GetAllBehavior();
            finalBehavior.Items.Add("None");
            for (int k = 0; k < behaviors.Count; k++)
            {
                finalBehavior.Items.Add(behaviors[k].Name);
            }
        }

        private void LoadDialogs()
        {
            
        }

        void WaypointsConfiguration_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && !wantToClose)
            {
                result = System.Windows.Forms.DialogResult.Cancel;
            }
        }

        private bool CheckSelected()
        {
            return actionSelector.SelectedIndex != -1;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckSelected())
            {
                selectedAction = LevelEditorDAO.GetInstance().getWalkBehavior( actionSelector.Items[actionSelector.SelectedIndex].ToString());

                if (finalBehavior.SelectedIndex != -1)
                if (finalBehavior.Items[finalBehavior.SelectedIndex].ToString() != "None")
                    selectedFinalBehavior = LevelEditorDAO.GetInstance().getWalkBehavior(finalBehavior.Items[finalBehavior.SelectedIndex].ToString());

                //selectedDialog = LevelEditorDAO.GetInstance().getBehavior(

                waypoints.Name = waypointsName.Text;
                if (selectedFinalBehavior != null)
                    waypoints.IdFinalBehavior = selectedFinalBehavior.ID;
                //waypoints.FinalBehavior = selectedFinalBehavior;

                result = System.Windows.Forms.DialogResult.OK;
                wantToClose = true;
                this.Close();
            }

        }

        public new DialogResult ShowDialog()
        {
            base.ShowDialog();

            return result;
        }

        private void actionSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Tile.CurrentSelectedTile == null) return;

            string behaviorName = actionSelector.Items[actionSelector.SelectedIndex].ToString();

            Behavior sB = Tile.CurrentSelectedTile.standAloneBehaviors.Find(delegate(Behavior b)
            {
                return b.Name.Equals(behaviorName);
            });
            if (sB == null)
            {
                waypointsName.Text = "";
                return;
            }

            if (sB.Waypoints != null && sB.Waypoints.Count != 0)
            {
                waypointsName.Text = sB.Waypoints.Name;
                finalBehavior.Items.Clear();

                if (sB.Waypoints.IdFinalBehavior > 0)
                {
                    finalBehavior.Items.Add(LevelEditorDAO.GetInstance().getBehavior(sB.Waypoints.IdFinalBehavior).Name);
                }

                //if (sB.Waypoints.FinalBehavior != null)
                //    finalBehavior.Items.Add(sB.Waypoints.FinalBehavior.Name);
                
                this.waypoints = sB.Waypoints;
                return;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            IsCreatingWaypointConfiguration = true;
            base.OnLoad(e);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            IsCreatingWaypointConfiguration = false;
            base.OnClosing(e);
        }
    }
}
